<template>
    <div class="header">
        <div class="title" :class="{ 'title-active': itemIndex === 1 }" @click="itemClick(1)">机械</div>
        <div class="title" :class="{ 'title-active': itemIndex === 2 }" @click="itemClick(2)">电器</div>
    </div>
    <tree :data="store.treeData" ref="treeCpnRef" @handlerNode="handlerNode" :expandedKey="store.expanded" placeholder="请输入设备名称" :treeProps="treeProps">
        <template #default="{ scope }">
            <div class="custom-tree-node">
                <template v-if="scope.data.depth !== '0'">
                    <i class="iconfont icon-wenjian icon"></i>
                    <img src="@/assets/image/report/child.png" class="child-img" />
                </template>
                <template v-else>
                    <img src="@/assets/image/report/parent.png" class="parent-img" />
                </template>
                <span class="label">{{ scope.data.name }}</span>
                <template v-if="scope.data.depth === '0'">
                    <div class="hot" v-if="scope.data.hot">{{ scope.data.hot }}</div>
                </template>
                <template v-else>
                    <div class="hot" v-if="scope.data.hot.length">{{ scope.data.hot.length }}</div>
                </template>
            </div>
        </template>
    </tree>
</template>

<script setup>
import tree from "@/components/tree.vue";
import { useReportOrderDetail } from "@/store/reportOrderDetail.js";
import { ref } from "vue";
import treeIcon from "@/components/treeIcon.vue";

const treeCpnRef = ref(null);
const store = useReportOrderDetail();
const treeProps = { label: "name", children: "childList" };
const emits = defineEmits(["handlerNode", "typeClick"]);
const handlerNode = (_, node) => {
    emits("handlerNode", node);
};
const itemIndex = ref(1);

const itemClick = (index) => {
    itemIndex.value = index;
    emits("typeClick", index);
};
defineExpose({
    itemIndex,
    treeCpnRef
});
</script>

<style lang="scss" scoped>
.header {
    display: flex;
    .title {
        color: #8c8c8c;
        cursor: pointer;
        &:last-child {
            margin-left: 16px;
        }
    }
    .title-active {
        font-weight: 600;
        color: #1b3646;
    }
}
.custom-tree-node {
    display: flex;
    align-items: center;
    .icon {
        position: relative;
        margin-right: 7px;
        font-size: 14px;
        color: #ebebeb;
    }
    .select {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 24px;
        height: 24px;
        font-size: 14px;
        color: #ebebeb;
    }
    .child-img {
        width: 15px;
        height: 16px;
    }
    .parent-img {
        width: 18px;
        height: 17px;
    }
    .label {
        margin-left: 9px;
    }
    .hot {
        margin-left: 4px;
        width: 16px;
        height: 16px;
        line-height: 16px;
        text-align: center;
        border-radius: 50%;
        font-size: 10px;
        background-color: #ffdbdc;
        color: #ff4d4f;
    }
}
</style>
